package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.LogisticsEntity;
import com.zw.yzk.kabu.data.network.task.LogisticsTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.LogisticsMapper;
import com.zw.yzk.kabu.view.view.LogisticsView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/18.
 */

public class LogisticsPresenter implements Presenter {

    private UseCase getLogisticsInfo;
    private AppCompatActivity activity;
    private LogisticsMapper mapper;
    private LogisticsView logisticsView;

    @Inject
    public LogisticsPresenter(@Named("logisticsUseCase") UseCase getLogisticsInfo,
                              LogisticsMapper mapper,
                              AppCompatActivity activity) {
        this.getLogisticsInfo = getLogisticsInfo;
        this.mapper = mapper;
        this.activity = activity;
    }

    public void setView(LogisticsView view) {
        this.logisticsView = view;
    }

    public void getLogisticsInfo(String id) {
        getLogisticsInfo.execute(new GetLogisticsSubscriber(activity), new LogisticsTask(id));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getLogisticsInfo.unSubscribe();
    }

    private class GetLogisticsSubscriber extends BaseSubscriber<LogisticsEntity> {

        public GetLogisticsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(LogisticsEntity logisticsEntity) {
            super.onNext(logisticsEntity);
            logisticsView.getLogisticsSuccess(mapper.transfer(logisticsEntity.result.traceList));
            logisticsView.setLogisticId(logisticsEntity.result.logisticCode);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_logistics_failed);
        }
    }
}
